﻿using Gumini.JulaDB.Enums;
using Gumini.JulaDB.Provider;
using Gumini.JulaDB.Test.Common;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Gumini.JulaDB.Test
{
    /// <summary>
    /// A set of tests for Data Definition Language parts of JulaDB SQL - creating and dropping tables.
    /// </summary>
    [TestClass]
    public class CreateDropTableTest
    {
        private static JulaDbConnection _connection;

        [TestInitialize]
        public void InitializeTest()
        {
            // database is initialized before every test to discard any changes made by previous tests
            _connection = TestUtils.CreateTestDatabase();
        }

        [TestCleanup]
        public void CleanupTest()
        {
            _connection.Close();
        }

        /// <summary>
        /// Create a table that already exists. Should return an error.
        /// </summary>
        [TestMethod]
        public void CreateExistingTable()
        {
            _connection.ExecuteInvalidCommand("create table dual (col varchar(100))", ErrorCode.DuplicateTableName);
        }

        /// <summary>
        /// Create a table with no columns. Should return an error.
        /// </summary>
        [TestMethod]
        public void CreateTableNoColumns()
        {
            _connection.ExecuteInvalidCommand("create table table_no_columns ()", ErrorCode.SyntaxError);
        }

        /// <summary>
        /// Create a table with two columns with the same name. Should return an error.
        /// </summary>
        [TestMethod]
        public void CreateTableDuplicateColumnNames()
        {
            _connection.ExecuteInvalidCommand("create table table_duplicate_columns (a int, a varchar(100))", ErrorCode.DuplicateColumnName);
        }

        /// <summary>
        /// Attempting to query a table which was already dropped. This is not allowed and should return an error.
        /// </summary>
        [TestMethod]
        public void DropExistingTable()
        {
            _connection.ExecuteValidSelect("select * from dual");
            _connection.ExecuteValidNonQuery("drop table dual");
            _connection.ExecuteInvalidCommand("select * from dual", ErrorCode.UnknownTable);
        }

        /// <summary>
        /// Attempting to drop a table which does not exist. This should return an error.
        /// </summary>
        [TestMethod]
        public void DropUnknownTable()
        {
            _connection.ExecuteInvalidCommand("drop table undefined_table", ErrorCode.UnknownTable);
        }
    }
}
